import { StateStorage, createJSONStorage } from 'zustand/middleware';

const storage: StateStorage = {
  getItem: (key: string) => {
    try {
      const value = localStorage.getItem(key);
      return value ?? null;
    } catch {
      return null;
    }
  },
  setItem: (key: string, value: string) => {
    try {
      localStorage.setItem(key, value);
    } catch {
      console.warn('Failed to save state to localStorage');
    }
  },
  removeItem: (key: string) => {
    try {
      localStorage.removeItem(key);
    } catch {
      console.warn('Failed to remove state from localStorage');
    }
  },
};

export const customStorage = createJSONStorage(() => storage); 